package com.ssm.curd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.curd.bean.Employee;
import com.ssm.curd.service.EmployeeService;
import com.ssm.curd.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 表控制层
 * @author zhaojiulin
 * @since 2019-11-28 15:32:40
 */
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    private final EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }


    /**
     * 通过页码或者
     * @param pn
     * @return 列表数据
     */
    @RequestMapping("/list")
    public Result selectAllList(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn,5);
        List<HashMap> list= employeeService.getAll();
        PageInfo<HashMap> pageInfo = new PageInfo<>(list);
        return Result.page(pageInfo);
    }

    /**
     * 保存员工信息
     * @param employee 员工封装信息
     * @return Result.success();
     */
    @PostMapping(value = "emp")
    public Result saveEmp(Employee employee){
        employeeService.saveEmp(employee);
        return Result.success();
    }

}